package cn.safekeeper.guide.admin.controller.system;

import cn.safekeeper.guide.admin.constant.MsgObj;
import cn.safekeeper.guide.admin.constant.UserConstants;
import cn.safekeeper.guide.admin.controller.BaseController;
import cn.safekeeper.guide.admin.domain.entity.SysPost;
import cn.safekeeper.guide.admin.domain.page.TableDataInfo;
import cn.safekeeper.guide.admin.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位信息操作处理
 */
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {
    @Autowired
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysPost post) {
        startPage();
        List<SysPost> list = postService.selectPostList(post);
        return getDataTable(list);
    }


    /**
     * 根据岗位编号获取详细信息
     */
    @GetMapping("/query")
    public MsgObj getInfo(Long postId) {
        return MsgObj.success(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PostMapping("/add")
    public MsgObj add(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return MsgObj.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return MsgObj.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(getUsername());
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @PutMapping("/edit")
    public MsgObj edit(@Validated @RequestBody SysPost post) {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return MsgObj.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return MsgObj.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(getUsername());
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @DeleteMapping("/remove")
    public MsgObj remove(Long[] postIds) {
        return toAjax(postService.deletePostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public MsgObj optionselect() {
        List<SysPost> posts = postService.selectPostAll();
        return MsgObj.success(posts);
    }
}
